#include "StdAfx.h"
#include "ship.h"
#include "planet.h"

const int shipwidth = 32;
const int shipheight = 32;
const QPen shipPen = QPen(QColor(0xff,0xff,0xff));
Ship::Ship(PlanetPtr destinationPlanet, const unsigned int countOfShips, const PlayerID playerID, 
           const qreal defaultAngle, bool notifyOnArrival, QGraphicsItem *parent)
           : QGraphicsObject(parent)
           , m_destinationPlanet(destinationPlanet)
           , m_sourcePlayerID(playerID)
           , m_countOfShips(countOfShips)
           , m_pixmap(":/galcon/Resources/images/spaceShip.png")
           , m_defaultRotationAngle(defaultAngle - 90)
           , m_notifyOnArrival(notifyOnArrival)
{
   m_pixmap = m_pixmap.scaled(shipwidth, shipheight);
   rotate(m_defaultRotationAngle);
   translate(-m_pixmap.width()/2, -m_pixmap.height()/2);
}

Ship::~Ship()
{
   disconnectSignals();
}

void Ship::paint( QPainter *painter, const QStyleOptionGraphicsItem *, QWidget * /* = 0 */ )
{
   painter->drawPixmap(0,0,m_pixmap);

   if (collidesWithItem(m_destinationPlanet.get()))
   {
      emit arrivedToPlanet(shared_from_this(), m_destinationPlanet, m_countOfShips, m_sourcePlayerID);
   }
}

QRectF Ship::boundingRect() const
{
   return QRectF(-1,-1, shipwidth + 1, shipheight + 1);
}

void Ship::disconnectSignals()
{
   disconnect();
   dumpObjectInfo();
}